package com.shujia;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Demo18WordCountJava {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("Scala/data/words.txt"));
        String line;
        HashMap<String, Integer> hMap = new HashMap<>();

        while ((line = br.readLine()) != null) {
            String[] splits = line.split(",");
            for (String word : splits) {
                // 如果第一次取到word单词 则直接（word作为key，1作为value）放入HashMap中
                if (!hMap.containsKey(word)) {
                    hMap.put(word, 1);
                } else {
                    // 如果单词已经放如果HashMap中 则将对应的value取出来 加1再放回去
                    hMap.put(word, hMap.get(word) + 1);
                }

            }

        }
        br.close();
        for (Map.Entry<String, Integer> entry : hMap.entrySet()) {
            System.out.println(entry.getKey() + "," + entry.getValue());
        }


    }
}
